/*
 *  xfelp-browser.c - This file is part of Xfelp
 *
 *  Copyright (C) 2008 Daniel Morales <daniel@daniel.com.uy>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>

#include <webkit/webkitwebview.h>

#include "xfelp-main.h"
#include "xfelp-window.h"
#include "xfelp-browser.h"

static void   xfelp_browser_class_init    (XfelpBrowserClass  *klass);
static void   xfelp_browser_init          (XfelpBrowser       *browser);
static void   xfelp_browser_finalize      (GObject            *object);

static void   xfelp_browser_title_changed (WebKitWebView      *page,
                                           WebKitWebFrame     *frame,
                                           const gchar        *page_title, 
                                           XfelpBrowser       *browser);

struct _XfelpBrowserClass
{
  WebKitWebViewClass __parent__;
};

struct _XfelpBrowser
{
  WebKitWebView  __parent__;
};

G_DEFINE_TYPE (XfelpBrowser, xfelp_browser, WEBKIT_TYPE_WEB_VIEW);

static void
xfelp_browser_class_init (XfelpBrowserClass *klass)
{
  GObjectClass   *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = xfelp_browser_finalize;
}

static void
xfelp_browser_init (XfelpBrowser *browser)
{
  /* Connect signals */
  g_signal_connect (browser, "title-changed", G_CALLBACK (xfelp_browser_title_changed), browser);
}

static void
xfelp_browser_finalize (GObject *object)
{
  //XfelpBrowser *browser = XFELP_BROWSER (object);

  /* Cleanup... */

  (*G_OBJECT_CLASS (xfelp_browser_parent_class)->finalize) (object);
}

static void
xfelp_browser_title_changed (WebKitWebView  *page,
                             WebKitWebFrame *frame,
                             const gchar    *page_title, 
                             XfelpBrowser    *browser)
{
  XfelpWindow *window = xfelp_window_get_default ();

  xfelp_window_set_title (window, page_title);
 
  g_object_unref (window);
}

GtkWidget *
xfelp_browser_new (void)
{
  GtkWidget *browser;

  browser = g_object_new (XFELP_TYPE_BROWSER, NULL);

  return browser;
}
